
//
//  ExercisesFeedbackTypeView.m
//  ShowYork
//
//  Created by hxh on 2020/9/13.
//  Copyright © 2020 hxh. All rights reserved.
//

#define itemH 60
#define itemLineH 8


#import "ExercisesFeedbackTypeView.h"

typedef void(^doneBlock)(void);

@interface ExercisesFeedbackTypeView ()
@property (nonatomic,strong)UIView *containView;
@property (nonatomic,strong)UIButton *confirmBtn;
@property (nonatomic,strong)UIButton *cancelBtn;
@property (nonatomic,strong)UIView *line;
@property (nonatomic,copy)doneBlock done;
@property (nonatomic,strong)UIView *bottomView;
@end

@implementation ExercisesFeedbackTypeView

- (instancetype)initWithCompleteBlock:(void (^)(void))completeBlock{
    if (self = [super init]) {
        [self setUI];
        if (completeBlock) {
            self.done = ^{
                completeBlock();
            };
        }
    }
    return self;
}

- (void)setUI{
    self.frame = CGRectMake(0, 0, kScreenW, kScreenH);
    self.backgroundColor = RGBA(0, 0, 0, 0.4);
    
    self.containView = [[UIView alloc]init];
    
    [self addSubview:self.containView];
    
    [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self).offset(itemH * 2 + itemLineH + GK_SAVEAREA_BTM);
        make.height.mas_equalTo(itemH * 2 + itemLineH + GK_SAVEAREA_BTM);
    }];
    
    self.confirmBtn = [[UIButton alloc]init];
    self.confirmBtn.backgroundColor = [UIColor whiteColor];
    [self.confirmBtn setTitle:@"题目反馈" forState:UIControlStateNormal];
    [self.confirmBtn setTitleColor:RGBA(50, 50, 50, 1) forState:UIControlStateNormal];
    self.confirmBtn.titleLabel.font = font(18);
    [self.confirmBtn addTarget:self action:@selector(confirmMethod) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.confirmBtn];
    
    [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.containView);
        make.height.mas_equalTo(60);
    }];
    
    self.line = [[UIView alloc]init];
    self.line.backgroundColor = RGBA(248, 248, 248, 1);
    [self addSubview:self.line];
    
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.containView);
        make.top.equalTo(self.confirmBtn.mas_bottom);
        make.height.mas_equalTo(8);
    }];
    
    self.cancelBtn = [[UIButton alloc]init];
    self.cancelBtn.backgroundColor = [UIColor whiteColor];
    [self.cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancelBtn setTitleColor:RGBA(50, 50, 50, 1) forState:UIControlStateNormal];
    self.cancelBtn.titleLabel.font = font(18);
    [self.cancelBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    [self.containView addSubview:self.cancelBtn];
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.line.mas_bottom);
        make.left.mas_equalTo(self.containView);
        make.right.mas_equalTo(self.containView);
        make.height.mas_equalTo(60);
    }];
    
    self.bottomView = [[UIView alloc]init];
    self.bottomView.backgroundColor = [UIColor whiteColor];
    [self.containView addSubview:self.bottomView];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.containView);
        make.top.equalTo(self.cancelBtn.mas_bottom);
    }];
    
}
- (void)show{
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    [self layoutIfNeeded];
      
    [self.containView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self);
    }];
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    }];
}
- (void)confirmMethod{
    [self layoutIfNeeded];
      
    [self.containView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(itemH * 2 + itemLineH + GK_SAVEAREA_BTM);
    }];
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
        if (self.done) {
            self.done();
        }
        
    }];
}

- (void)dismiss{
    [self.containView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(itemH * 2 + itemLineH + GK_SAVEAREA_BTM);
    }];
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }        
    }];
}

- (void)drawRect:(CGRect)rect{
    [super drawRect:rect];

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.confirmBtn.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(14, 14)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.confirmBtn.bounds;
    maskLayer.path = maskPath.CGPath;
    self.confirmBtn.layer.mask = maskLayer;
    
    
}

@end
